<?php
namespace frontend\controllers;

use frontend\models\DisUpload;
use frontend\models\DisWorksInfo;
use frontend\models\ImgUploadForm;
use frontend\models\ZipUploadForm;
use yii\helpers\Html;
use yii\helpers\Url;
use yii\web\Controller;
use yii\web\Response;
use yii\web\UploadedFile;

class UploadController extends Controller
{
    public $enableCsrfValidation = false;

    private $ignoreList = ['index', 'intro'];

    public function beforeAction($action)
    {
        if (\Yii::$app->user->isGuest)
        {
            if (in_array($action->id, $this->ignoreList))
            {
                return true;
            } else
            {
                $this->redirect(['user/login']);
                return false;
            }
        }
        return true;
    }

    /**
     *
     * 上传压缩文件控制

     * @return array
     */
    public function actionZip()
    {
        $model=new ZipUploadForm();
        $timestamp=\Yii::$app->request->post('timestamp');
        $token=\Yii::$app->request->post('token');
        if(\Yii::$app->request->isPost && $token==md5("unique_salt" . md5($timestamp)))
        {
            $model->file_upload=UploadedFile::getInstanceByName('Filedata');
            if($model->validate())
            {
                $filename=md5($model->file_upload->baseName.time());
                $model->file_upload->saveAs('uploads/zip/'.$filename.'.'.$model->file_upload->extension);
                $record=[];
                $record['type']='zip';
                $record['url']=\Yii::$app->request->hostInfo . Url::to('@web/uploads/zip/'.$filename.'.'.$model->file_upload->extension);
                $file=new DisUpload();
                $file->urls=json_encode($record);
                $arr=\Yii::$app->getSession()['zip_tmp'];
                if(!isset($arr))
                {
                    $arr=[];
                }
                array_push($arr,$file);
                \Yii::$app->getSession()->set('zip_tmp',$arr);
                //\Yii::$app->getSession()->setFlash('upload_success','文件上传成功！');
                //\Yii::$app->getSession()->setFlash('up_zip_filename',$filename.'.'.$model->file_upload ->extension);
                \Yii::$app->response->format=Response::FORMAT_JSON;
                return ['state'=>'success'];
            }
            else
            {
                \Yii::$app->response->format=Response::FORMAT_JSON;
                return ['state'=>'error','msg'=>'非法的文件格式！'];
            }
        }else
            \Yii::$app->response->format=Response::FORMAT_JSON;
        return ['state'=>'error','msg'=>'wrong token!!!'];
    }

    /**
     * 上传图片控制器
     * @return array
     */
    public function actionImg()
    {
        $model=new ImgUploadForm();
        $timestamp=\Yii::$app->request->post('timestamp');
        $token=\Yii::$app->request->post('token');
        if(\Yii::$app->request->isPost && $token==md5("unique_salt" . md5($timestamp)))
        {
            $model->file_upload=UploadedFile::getInstanceByName('Filedata');
            if($model->validate())
            {
                $filename=md5($model->file_upload->baseName.time());
                $model->file_upload->saveAs('uploads/img/'.$filename.'.'.$model->file_upload->extension);
                $record=[];
                $record['type']='img';
                $record['url']=\Yii::$app->request->hostInfo . Url::to('@web/uploads/img/'.$filename.'.'.$model->file_upload->extension);
                $file=new DisUpload();
                $file->urls=json_encode($record);
                $arr=\Yii::$app->getSession()['img_tmp'];
                if(!isset($arr))
                {
                    $arr=[];
                }
                array_push($arr,$file);
                \Yii::$app->getSession()->set('img_tmp',$arr);
                //\Yii::$app->getSession()->setFlash('upload_success','文件上传成功！');
                //\Yii::$app->getSession()->setFlash('up_zip_filename',$filename.'.'.$model->file_upload ->extension);
                \Yii::$app->response->format=Response::FORMAT_JSON;
                return ['state'=>'success'];
            }
            else
            {
                \Yii::$app->response->format=Response::FORMAT_JSON;
                return ['state'=>'error','msg'=>'非法的文件格式！'];
            }
        }else
            \Yii::$app->response->format=Response::FORMAT_JSON;
        return ['state'=>'error','msg'=>'wrong token!!!'];
    }

    public function actionView($class_id)
    {
        $model=DisWorksInfo::find()
            ->where(['user_id'=>\Yii::$app->user->id,'works_class1_id'=>$class_id])
            ->one();
        if($model)
        {
            $uploads=$model->getDisUploads()->all();
            if (!empty($uploads))
            {
                $html="";
                foreach ($uploads as $key=>$upload)
                {
                    $up=json_decode($upload->urls,true);
                    if($up['type']=='img')
                    {
                        $html.=Html::a(Html::img($up['url']),$up['url']);
                    }elseif($up['type']=='zip')
                    {
                        $html.=Html::a('附件下载'.$key,$up['url']);
                    }
                }
                return $html;
            }else
            {
                return Html::tag('p','没有附件');
            }
        }
        else
        {
            return Html::tag('p','不存在的 tan 90°,我猜你没有报名！');
        }
    }
}